package com.jcsz.gulimall.product.service.impl;

import com.jcsz.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.jcsz.gulimall.product.dao.AttrDao;
import com.jcsz.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.jcsz.gulimall.product.entity.AttrEntity;
import com.jcsz.gulimall.product.service.AttrService;
import com.jcsz.gulimall.product.service.CategoryService;
import com.jcsz.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.jcsz.gulimall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcsz.common.utils.PageUtils;
import com.jcsz.common.utils.Query;

import com.jcsz.gulimall.product.dao.AttrGroupDao;
import com.jcsz.gulimall.product.entity.AttrGroupEntity;
import com.jcsz.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCatId(Map<String, Object> params, Long catId) {
        QueryWrapper<AttrGroupEntity> entityQueryWrapper = new QueryWrapper<>();
        Object key = params.get("key");
        if (!StringUtils.isEmpty(key)) {
            entityQueryWrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        IPage page;
        if (catId == 0) {
            page = this.page(new Query<>().getPage(params), entityQueryWrapper);
        } else {
            entityQueryWrapper.eq("catelog_id", catId);
            page = this.page(new Query<>().getPage(params), entityQueryWrapper);
        }
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> queryAttrs(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<AttrEntity> attrEntities = null;
        if (attrAttrgroupRelationEntities.size() > 0) {
            List<Long> ids = attrAttrgroupRelationEntities.stream().map((obj) -> obj.getAttrId()).collect(Collectors.toList());

            attrEntities = attrService.listByIds(ids);
        }

        return attrEntities;
    }

    @Override
    public PageUtils queryNoAttrs(Map<String, Object> params, Long attrgroupId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((obj) -> {
                obj.eq("attr_id", key).or().like("attr_name", key);
            });
        }

        Integer selectCount = attrDao.selectCount(new QueryWrapper<AttrEntity>().eq("catelog_id", this.getById(attrgroupId).getCatelogId()));
        if (selectCount > 0) {
            List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().ne("attr_group_id", attrgroupId));
            if (attrAttrgroupRelationEntities.size() != 0) {
                List<Long> ids = attrAttrgroupRelationEntities.stream().map((obj) -> obj.getAttrId()).collect(Collectors.toList());
                queryWrapper.in("attr_id", ids);
                IPage page = attrService.page(new Query<>().getPage(params), queryWrapper);
                return new PageUtils(page);
            }

        }
        return null;
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrByCategoryId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupEntities.stream().map(obj -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(obj, attrGroupWithAttrsVo);

            List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", obj.getAttrGroupId()));
            if (attrAttrgroupRelationEntities.size() != 0) {
                List<Long> ids = attrAttrgroupRelationEntities.stream().map(item -> item.getAttrId()).collect(Collectors.toList());

                List<AttrEntity> attrEntities = attrService.listByIds(ids);

                attrGroupWithAttrsVo.setAttrs(attrEntities);

            }

            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return attrGroupWithAttrsVos;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrBySpuId(Long spuId, Long catalogId) {

        return baseMapper.getAttrGroupWithAttrBySpuId(spuId,catalogId);
    }
}